
*=============================================================================*
	capture log close
	clear
	set more off
	
	global wealthdon /* SET FILE PATH */
	global outputfilename "wdon1_donations_person_year.dta"

	global raw 				 ${wealthdon}/data/raw
	global projectdatafolder ${wealthdon}/data/projectdata

*=============================================================================*

disp "[$S_DATE $S_TIME] Starting"


*=============================================================================*
* Prep gift data
*=============================================================================*


use ${raw}/gaver_g2012.dta, clear
append using ${raw}/gaver_g2013.dta
append using ${raw}/gaver_g2014.dta
append using ${raw}/gaver_g2015.dta
append using ${raw}/gaver_g2016.dta
append using ${raw}/gaver_g2017.dta
replace belop = BELOP if aar =="2017"
drop BELOP
append using ${raw}/gaver_g2018.dta
destring aar, replace
ren aar year
drop ordning /* info in "type" */
replace orgnr = oppgavegiver if year ==2018
replace LOPNR = oppgaveeier if year==2018
drop oppgavegiver oppgaveeier
drop if real(LOPNR)<01010000000


*=============================================================================*
* Type of charity
*=============================================================================*

desc using ${projectdatafolder}/don_nace.dta
destring orgnr, replace
merge m:1 orgnr using ${projectdatafolder}/don_nace.dta
tab year _merge
drop if _merge==2
drop _merge
format orgnr %12.0g

*--------------------------------------------*
* Manually fix nace for all org > 100 obs
*--------------------------------------------*
replace nace = "88.995" if orgnr==974292262

replace orgnr= 915972438 if orgnr==961331846
replace nace= "94.992" if orgnr==961331846

replace nace ="94.910" 	if orgnr==970550259

replace nace ="88.995" 	if orgnr==971038179
replace orgnr= 974292262 if orgnr==971038179

replace nace ="94.910" 	if orgnr==970443010
replace nace ="94.910" 	if orgnr==973732412
replace nace ="85.429" 	if orgnr==995189186
replace nace ="94.910" 	if orgnr==970011994
replace nace ="94.910" 	if orgnr==989333895
replace nace ="94.910" 	if orgnr==973259415
replace nace ="94.992" 	if orgnr==987052244
replace nace ="94.910" 	if orgnr==960496582
replace nace ="88.999" 	if orgnr==981975677
replace nace ="94.910" 	if orgnr==989205544
replace nace ="88.999" 	if orgnr==945712104
replace nace ="88.999" 	if orgnr==980857921
replace nace ="85.593" 	if orgnr==917387079
replace nace ="88.995" 	if orgnr==986625372
replace nace ="85.422" 	if orgnr==915635520

replace orgnr=973257250	if orgnr==995640384
replace nace="85.429"	if orgnr==995640384

replace nace ="94.120" 	if orgnr==982022169
replace nace ="85.320" 	if orgnr==989041819
replace nace ="72.190" 	if orgnr==977143578
replace nace ="" 	if orgnr==600025

replace nace ="85.429" 	if orgnr==974157055
replace nace ="85.429"	if orgnr==914086434
replace nace ="94.992" 	if orgnr==971278471
replace nace ="94.992" 	if orgnr==976129032
replace nace ="94.992" 	if orgnr==976715381
replace nace ="94.910" 	if orgnr==980080722
replace nace ="94.910" 	if orgnr==977119294
replace nace ="94.992" 	if orgnr==977128595
replace nace ="94.991" 	if orgnr==977141222
replace nace ="94.910" 	if orgnr==871462402
replace nace ="94.992" 	if orgnr==992967897
replace nace ="94.910" 	if orgnr==988842389
replace nace ="94.992" 	if orgnr==977142946

* Replace nace desc with nonmiss
bysort nace: egen nace_desc2 = mode(nace_desc)
drop nace_desc
ren nace_desc2 nace_desc


drop if real(LOPNR)<01010000000



merge m:1 orgnr using ${raw}/charitable_org_descriptions.dta, update replace
drop _merge
ren Type CharityType
replace CharityType = "Klima" if orgnr==952330071 /* WWF */
replace CharityType = "Menneskerettigheter" if CharityType == "Amnesty"
*=============================================================================*

*=============================================================================*

* aggregate to LOPNR year level
/* NB, later want to do this by type of charity as well */
bysort LOPNR year: egen gaver_tot 	= total( belop *(type=="1")								),mi
bysort LOPNR year: egen gaver_rel 	= total( belop *(type=="1") * (CharityType=="Kristen" | CharityType=="Religion")	),mi
bysort LOPNR year: egen gaver_nonrel 	= total( belop *(type=="1") * (CharityType!="Kristen" & CharityType!="Religion") 	),mi
bysort LOPNR year: egen gaver_aid 	= total( belop *(type=="1") * (CharityType=="Bistand") 					),mi
bysort LOPNR year: egen gaver_medical 	= total( belop *(type=="1") * (CharityType=="Sykdom") 					),mi
bysort LOPNR year: egen gaver_social 	= total( belop *(type=="1") * (CharityType=="Sosial") 					),mi
bysort LOPNR year: egen gaver_climate 	= total( belop *(type=="1") * (CharityType=="Klima") 					),mi
bysort LOPNR year: egen gaver_humrig 	= total( belop *(type=="1") * (CharityType=="Menneskerettigheter") 			),mi
bysort LOPNR year: egen gaver_domserv 	= total( belop *(type=="1") * (CharityType=="InnenlandsTjenester") 			),mi
		  gen gaver_rest	= gaver_tot - gaver_rel - gaver_aid - gaver_medical -gaver_social -gaver_climate -gaver_humrig -gaver_domserv
bysort LOPNR year: egen forskning 	= total( belop *(type!="1") 								),mi

bysort LOPNR year: gen dupn=_n
keep if dupn==1
keep LOPNR year gaver_* forskning



*=============================================================================*
* 
*=============================================================================*

*=============================================================================*
* Destring
*=============================================================================*



*=============================================================================*
* Household stuff and cleaning
*=============================================================================*


*=============================================================================*
* Save
*=============================================================================*



compress

save ${projectdatafolder}/${outputfilename}, replace

disp "[$S_DATE $S_TIME] Finished"

